<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
class Rolelist extends Allow
{
    public function getindex()
    {
        //获取role表用户信息
        $users=Db::table("role")->select();
    	//加载后台角色模板
    	return $this->fetch("Rolelist/index",['users'=>$users]);
    }

    //添加
    public function getadd(){
        //加载角色添加模板
        return $this->fetch("Rolelist/add");
    }

    //执行添加
    public function postinsert(){
        $request=request();
        //获取表单传过来的值\
        $data=$request->only(['name']);
        //判断值是否为空
        if(!empty($data['name'])){
            //执行添加
            if(Db::table('role')->insert($data)){
                $this->success("添加成功","/adminrolelist/index");
            }else{
                $this->error("添加失败","/adminrolelist/add");
            }
        }else{
                $this->error("添加角色不能为空","/adminrolelist/add");
        }
    }

    //删除
    public function getdelete(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //执行删除
        if(Db::table('role')->where('id',"{$id}")->delete() && Db::table('role_node')->where('rid',"{$id}")->delete()){
            $this->success("删除成功","/adminrolelist/index");
        }else{
            $this->error("删除失败","/adminrolelist/index");

        }
    }

    //修改
    public function getedit(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取需要编辑的数据
        $role=Db::table('role')->where("id","{$id}")->find();
        return $this->fetch("Rolelist/edit",['role'=>$role]);
    }

    //执行修改
    public function postupdate(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取需要修改的数据
        $name=$request->only(['name']);
        if(Db::table('role')->where("id","{$id}")->update($name)){
            $this->success("修改成功","/adminrolelist/index");
        }else{
            $this->error("修改失败","/adminrolelist/edit");
        }
    }

    //分配权限
    public function getnodes(){
        $request=request();
        //获取rid
        $rid=$request->param('id');
        //查询node表所有数据
        $nodes=Db::table('node')->select();
        //获取当前角色所具有的权限信息
        $data=Db::table('role_node')->where("rid","{$rid}")->select();
        //获取权限id  存储在数组里
        //遍历
        foreach($data as $v){
           $nids[]=$v['nid'];
        }
        return $this->fetch("Rolelist/nodes",['nodes'=>$nodes,'rid'=>$rid,'nids'=>$nids]);
    }

    //保存权限
    public function postrolenode(){
        $request=request();
        //获取角色id
        $rid=$request->param('rid');
        //把当前角色已有的角色信息删除掉
        Db::table('role_node')->where("rid","{$rid}")->delete();
        //获取权限id
        $nid=$_POST['nid'];
        //遍历数组$nid
        foreach($nid as $v){
            $data['rid']=$rid;//角色id
            $data['nid']=$v;//权限id
            //执行插入
            Db::table('role_node')->insert($data);
        }
        $this->success("权限分配成功","/adminrolelist/index");
    }

    //修改角色状态
    public function getstatus(){
        $request=request();
        //获取需要修改角色的id
        $rid=$request->param('id');
        //获取需要修改状态的值
        $status=$request->param('status');
        if(Db::execute("update role set status={$status} where id={$rid}")){
            $this->redirect("/adminrolelist/index");
        }
    }
}
